package view;
import utils.Tools;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class GroupListView {
    private JFrame frame;
    private final int WIDTH = 600;    // 加宽窗口以便显示列表
    private final int HEIGHT = 400;
    private JPanel listPanel;         // 用于显示小组列表的面板

    public GroupListView() {
        // 修改窗口标题为"小组列表"
        frame = new JFrame("小组列表");
        initView();
        frame.setLayout(null);
        frame.setResizable(false);
        Tools.setPos(frame, WIDTH, HEIGHT);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }

    private void initView() {
        // 创建标题标签
        JLabel titleLabel = new JLabel("当前所有小组", SwingConstants.CENTER);
        titleLabel.setBounds(0, 20, WIDTH, 30);
        titleLabel.setFont(new Font("宋体", Font.BOLD, 16));
        frame.add(titleLabel);

        // 创建滚动面板来容纳小组列表
        listPanel = new JPanel();
        //y轴滚动布局
        listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.Y_AXIS));
        JScrollPane scrollPane = new JScrollPane(listPanel);
        scrollPane.setBounds(50, 60, WIDTH-100, HEIGHT-150);
        frame.add(scrollPane);

        // 加载并显示小组列表
        loadGroupList();

        // 创建返回按钮
        JButton backBtn = new JButton("返回主页");
        backBtn.setBounds(WIDTH/2-50, HEIGHT-70, 100, 30);
        backBtn.addActionListener(e -> {
            frame.dispose();
            new MainView();
        });
        frame.add(backBtn);
    }

    // 加载小组列表
    private void loadGroupList() {
        try {
            File file = new File("resources/Group.txt");
            if (!file.exists()) {
                JLabel noDataLabel = new JLabel("暂无小组信息");
                //组件水平居中显示
                noDataLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
                listPanel.add(noDataLabel);
                return;
            }

            BufferedReader br = new BufferedReader(new FileReader(file));
            String GroupName;
            while ((GroupName = br.readLine()) != null) {
                if (!GroupName.trim().isEmpty()) {
                    //显示小组
                    addGroupItem(GroupName.trim());
                }
            }
            br.close();

        } catch (IOException e) {
            JOptionPane.showMessageDialog(frame, "读取小组信息失败：" + e.getMessage(),
                    "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    // 为每个小组创建显示项
    private void addGroupItem(String GroupName) {
        // 创建单个小组项的面板
        JPanel itemPanel = new JPanel();
        itemPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        itemPanel.setMaximumSize(new Dimension(WIDTH-120, 40));

        // 小组名称标签
        JLabel nameLabel = new JLabel(GroupName);
        nameLabel.setPreferredSize(new Dimension(200, 30));

        // 删除按钮
        JButton deleteBtn = new JButton("删除");
        deleteBtn.addActionListener(e -> deleteGroup(GroupName, itemPanel));

        // 添加组件到面板
        itemPanel.add(nameLabel);
        itemPanel.add(deleteBtn);
        listPanel.add(itemPanel);
        listPanel.revalidate();
    }

    // 删除小组
    private void deleteGroup(String GroupName, JPanel itemPanel) {
        int confirm = JOptionPane.showConfirmDialog(frame,
                "确定要删除小组 " + GroupName + " 吗？",
                "确认删除",
                JOptionPane.YES_NO_OPTION);
        //确认提示框
        if (confirm == JOptionPane.YES_OPTION) {
            try {
                // 读取所有小组
                List<String> group = new ArrayList<>();
                BufferedReader br = new BufferedReader(new FileReader("resources/Group.txt"));
                String line;
                while ((line = br.readLine()) != null) {
                    if (!line.trim().equals(GroupName)) {
                        group.add(line);
                    }
                }
                br.close();

                // 重写文件
                BufferedWriter bw = new BufferedWriter(new FileWriter("resources/Group.txt"));
                //用于遍历数组、集合的增强性for循环，不用考虑数组越界
                for (String gro : group) {
                    bw.write(gro);
                    bw.newLine();
                }
                bw.close();

                // 从界面移除
                listPanel.remove(itemPanel);    // 从面板中移除被删除的项
                listPanel.revalidate();         // 重新验证布局
                listPanel.repaint();            // 重新绘制界面

                JOptionPane.showMessageDialog(frame, "删除成功！");

            } catch (IOException e) {
                JOptionPane.showMessageDialog(frame, "删除失败：" + e.getMessage(),
                        "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}

